package com.android.home.mvp.view.home;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.android.amap.AmapLocation;
import com.android.common.base.BaseActivity;
import com.android.common.base.BaseFragmentAdapter;
import com.android.common.constant.Constant;
import com.android.common.constant.IntentKey;
import com.android.common.utils.SpUtils;
import com.android.home.R;
import com.android.home.fragment.HomeFirstFragment;
import com.android.home.fragment.HomeSecondFragment;
import com.android.home.fragment.MyFragment;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity
    implements View.OnClickListener {
    /**
     * ViewPager
     */
    private ViewPager2 mVpMain;
    /**
     * default pagerIndex
     */
    private int mCurrentItem;
    //chat
    private RelativeLayout mRlChat;
    private ImageView mIvChat;
    private TextView mTvChat;
    //contact
    private RelativeLayout mRlContact;
    private ImageView mIvContact;
    //mine
    private RelativeLayout mRlMine;
    private ImageView mIvMine;
    private TextView mTvMine;
    //目前正在展现的tab
    private int mSelectTabIndex = -1;

    private int mTabCount = 3;

    private List<Fragment> mFragments;

    private ImageView mIvContainer[];


    @Override
    protected int onSetRootViewId() {
        return R.layout.activity_main;
    }


    @Override
    protected void onInitView() {
        super.onInitView();
        SpUtils.putString(Constant.TOKEN_KEY,Constant.TOKEN_KEY);
        mCurrentItem = mBundle.getInt(IntentKey.CURRENT_ITEM_KEY,Constant.ZERO);
        mVpMain = findViewById(R.id.vp_main);
        mVpMain.setUserInputEnabled(false);
        //chat
        mRlChat = findViewById(R.id.rl_first);
        mIvChat = findViewById(R.id.iv_first);
        mTvChat = findViewById(R.id.tv_first);
        //contact
        mRlContact = findViewById(R.id.rl_second);
        mIvContact = findViewById(R.id.iv_second);
        //mine
        mRlMine = findViewById(R.id.rl_third);
        mIvMine = findViewById(R.id.iv_third);
        mTvMine = findViewById(R.id.tv_third);
        setUpIvContainer();
    }

    private void setUpIvContainer() {
        mIvContainer = new ImageView[mTabCount];
        mIvContainer[0] = mIvChat;
        mIvContainer[1] = mIvContact;
        mIvContainer[2] = mIvMine;
    }

    private void setUpFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(HomeFirstFragment.getInstance());
        mFragments.add(HomeSecondFragment.getInstance());
        mFragments.add(MyFragment.getInstance());
        BaseFragmentAdapter mFragmentStateAdapter = new BaseFragmentAdapter(this, mFragments);
        mVpMain.setAdapter(mFragmentStateAdapter);
        mVpMain.setCurrentItem(mCurrentItem,false);
    }

    private void setSelected(int selectIndex) {
        int index = 0;
        for (ImageView iv : mIvContainer) {
            if (index == selectIndex) {
                iv.setSelected(true);
            } else {
                iv.setSelected(false);
            }
            index++;
        }

    }

    @Override
    protected void onInitEvent() {
        super.onInitEvent();
        mRlChat.setOnClickListener(this);
        mRlContact.setOnClickListener(this);
        mRlMine.setOnClickListener(this);
        AmapLocation.getInstance(this).startOnceLocation();
    }

    @Override
    protected void onInitFragment() {
        super.onInitFragment();
        setUpFragment();
        selectTab(0);
    }

    public void showViewPagerItem(int itemIndex){
        mVpMain.setCurrentItem(itemIndex,false);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_first){
            selectTab(0);
        }else if (view.getId() == R.id.rl_second){
            selectTab(1);
        }else if (view.getId() == R.id.rl_third){
            selectTab(2);
        }
    }

    private void selectTab(int tabIndex){
        if (tabIndex != mSelectTabIndex){
            setSelected(tabIndex);
            showViewPagerItem(tabIndex);
            mSelectTabIndex = tabIndex;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
